package com.yxkj.syh.app.huarong.activities.orders.pay_detail;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.ErrorResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.config.ResponseCode;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.adps.PayOrderDetail1Adp;
import com.yxkj.syh.app.huarong.bean.PayOrderDetailInfo;
import com.yxkj.syh.app.huarong.bean.PayOrderDetailResponse;
import com.yxkj.syh.app.huarong.bean.PayOrderInfo;
import com.yxkj.syh.app.huarong.constants.OrderStatus;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/6/13 9:30
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class PayOrderDetailVM extends BaseViewModel<ActivityEvent> {

    private PayOrderDetail1Adp mPayOrderDetail1Adp1 = new PayOrderDetail1Adp();
    private PayOrderDetail1Adp mPayOrderDetail1Adp2 = new PayOrderDetail1Adp();

    public PayOrderDetailVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofForm1 = new ObservableField<>();//总里程
    public ObservableField<String> ofForm2 = new ObservableField<>();//往 总支出
    public ObservableField<String> ofForm3 = new ObservableField<>();//返 总支出
    public ObservableField<String> ofForm4 = new ObservableField<>();//往返总合计
    public ObservableField<String> ofForm5 = new ObservableField<>();//加油升数
    public ObservableField<String> ofForm6 = new ObservableField<>();//加油单价
    public ObservableField<String> ofForm7 = new ObservableField<>();//前次里程公里数
    public ObservableField<String> ofForm8 = new ObservableField<>();//本次里程公里数
    public ObservableField<String> ofForm9 = new ObservableField<>();//本次油耗
    public ObservableField<String> ofForm10 = new ObservableField<>();//油费总金额
    public ObservableField<String> ofForm11 = new ObservableField<>();//总报款
    public ObservableField<String> ofForm12 = new ObservableField<>();//备注
    public ObservableField<String> ofForm13 = new ObservableField<>();//爆款时间

    public ObservableField<Boolean> ofReceiveMoney = new ObservableField<>(Boolean.FALSE);

    public MutableLiveData<PayOrderInfo> mldDetailInfo = new MutableLiveData<>();
    public MutableLiveData<Boolean> mldReceiveMoney = new MutableLiveData<>();

    public void payOrderDetail(long id) {
        OrderModel.getOrderModel().payOrderDetail(id, mLifecycleProvider.bindToLifecycle(), new ApiObserver<PayOrderDetailResponse>() {
            @Override
            public void onSuccess(PayOrderDetailResponse payOrderDetailResponse) {
                mldDetailInfo.setValue(payOrderDetailResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
                if (code == ResponseCode.ERROR_500)
                    mldDetailInfo.setValue(null);
            }
        });
    }

    public  void payOrderReceiveMoney(String id){
        Map<String,Object> pMap = new HashMap<>();
        pMap.put("expensesId",id);

        showLoading("payOrderReceiveMoney");
        OrderModel.getOrderModel().payOrderReceiveMoney(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("payOrderReceiveMoney");
                mldReceiveMoney.setValue(true);
                Tooast.success(response.getMessage());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("payOrderReceiveMoney");
                mldReceiveMoney.setValue(false);
            }
        });
    }

    public void setData(PayOrderInfo payOrderInfo) {

        ofForm1.set((payOrderInfo.getNowMileage() - payOrderInfo.getPreMileage()) + "km");

        List<PayOrderDetailInfo> infos1 = new ArrayList<>();
        List<PayOrderDetailInfo> infos2 = new ArrayList<>();
        BigDecimal count1 = BigDecimal.ZERO;
        BigDecimal count2 = BigDecimal.ZERO;
        for (PayOrderDetailInfo info : payOrderInfo.getExpensesDetailList()) {
            if (info.getDrivingType() == 0) {//往
                infos1.add(info);
                count1 = count1.add(info.getPrice());
            } else if (info.getDrivingType() == 1) {//返
                infos2.add(info);
                count2 = count2.add(info.getPrice());
            }
        }
        mPayOrderDetail1Adp1.setNewData(infos1);
        mPayOrderDetail1Adp2.setNewData(infos2);
        ofForm2.set(ComUtil.clearNoUseZero(count1).toPlainString() + "元");
        ofForm3.set(ComUtil.clearNoUseZero(count2).toPlainString() + "元");
        ofForm4.set(ComUtil.clearNoUseZero(count1.add(count2)).toPlainString() + "元");
        ofForm5.set(ComUtil.clearNoUseZero(payOrderInfo.getFuelConsumption()).toPlainString() + "L");
        ofForm6.set(ComUtil.clearNoUseZero(payOrderInfo.getFuelPrice()).toPlainString() + "元/L");
        ofForm7.set(payOrderInfo.getPreMileage() + "km");
        ofForm8.set(payOrderInfo.getNowMileage() + "km");
        ofForm9.set(ComUtil.clearNoUseZero(payOrderInfo.getAveConsumption()).toPlainString() + "L/km");
        ofForm10.set(ComUtil.clearNoUseZero(payOrderInfo.getTotalFuel()).toPlainString() + "元");
        ofForm11.set(ComUtil.clearNoUseZero(payOrderInfo.getRefundableFee()).toPlainString() + "元");
        ofForm12.set(payOrderInfo.getRemark());
        ofForm13.set(payOrderInfo.getUpdateTime());

        if (payOrderInfo.getStatus()== OrderStatus.PAY_WAIT_REFUND){
            ofReceiveMoney.set(true);
        }else {
            ofReceiveMoney.set(false);
        }
    }

    public PayOrderDetail1Adp getPayOrderDetail1Adp1() {
        return mPayOrderDetail1Adp1;
    }

    public PayOrderDetail1Adp getPayOrderDetail1Adp2() {
        return mPayOrderDetail1Adp2;
    }
}
